#include "centermainwidget.h"
#include "ui_centermainwidget.h"


centerMainWidget::centerMainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::centerMainWidget)
{
    ui->setupUi(this);
    m_pSqliteManage = new SqliteManage;
    m_pDataMonitor = new DataMonitor(this);
    m_pDeviceManage = new DeviceManage(this);
    m_pAdminDeviceManage = new AdminDeviceManage(this);
    m_pWarningManage = new WarningManage(this);
    m_pDataAnalysis = new DataAnalysis(this);
    m_pUserManage = new UserManage(this);
    m_pSysSettings = new SysSettings(this);
    m_pLogManage = new LogManage(this);
    m_pAdminUserManage = new AdminUserManage(this);

    ui->stackedWidget->addWidget(m_pDataMonitor);   //index = 0
    ui->stackedWidget->addWidget(m_pDeviceManage);  //index = 1
    ui->stackedWidget->addWidget(m_pWarningManage); //index = 2
    ui->stackedWidget->addWidget(m_pDataAnalysis);  //index = 3
    ui->stackedWidget->addWidget(m_pUserManage);    //index = 4
    ui->stackedWidget->addWidget(m_pSysSettings);   //index = 5
    ui->stackedWidget->addWidget(m_pLogManage);     //index = 6
    ui->stackedWidget->addWidget(m_pAdminUserManage);   //index = 7
    ui->stackedWidget->addWidget(m_pAdminDeviceManage); //  index = 8

    connect(ui->btnDataMonitor,&QPushButton::clicked,ui->stackedWidget,[=](){
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->btnWarnning,&QPushButton::clicked,ui->stackedWidget,[=](){
        ui->stackedWidget->setCurrentIndex(2);
    });
    connect(ui->btnDataAnalysis,&QPushButton::clicked,ui->stackedWidget,[=](){
        ui->stackedWidget->setCurrentIndex(3);
    });
    connect(ui->btnSysSettings,&QPushButton::clicked,ui->stackedWidget,[=](){
        ui->stackedWidget->setCurrentIndex(5);
    });
    connect(ui->btnLog,&QPushButton::clicked,ui->stackedWidget,[=](){
        ui->stackedWidget->setCurrentIndex(6);
    });
    connect(m_pDataMonitor,&DataMonitor::sendDataPoint,m_pWarningManage,[=](int &device_id,const DataPoint &data){
        m_pWarningManage->processDataPoint(device_id,data);
    });

}

centerMainWidget::~centerMainWidget()
{
    delete ui;
    m_pSqliteManage->closeDatabase();

}

void centerMainWidget::setUserInfo(QString username)
{
    if (!m_pSqliteManage->openDatabase("C:/Users/shafl/Desktop/FrameWork/test.db")) {
        qDebug() << "Failed to open database:" << m_pSqliteManage->lastError();
        emit sendMessage("数据库连接错误");
        return;
    }
    QList<QVariantMap> users = m_pSqliteManage->selectData("users","name='" + username + "'");
    foreach (const QVariantMap &user, users) {
        UserName = user["name"].toString();
        PassWord = user["passwd"].toString();
        user_role = user["role"].toString();
        email = user["email"].toString();
        phone = user["phone"].toString();
        nickname = user["nickname"].toString();
    }

    m_pDataMonitor->setUserName(UserName);
    m_pAdminDeviceManage->setUserName(UserName);
    m_pDeviceManage->setUserName(UserName);
    m_pWarningManage->setUserName(UserName);

    if(user_role == "admin"){
        connect(ui->btnUserManage,&QPushButton::clicked,ui->stackedWidget,[=](){
            ui->stackedWidget->setCurrentIndex(7);//设置用户管理页为管理员
        });
        connect(ui->btnDeviceManage,&QPushButton::clicked,ui->stackedWidget,[=](){
            ui->stackedWidget->setCurrentIndex(8);//设置设备管理页为管理员
        });
        m_pAdminUserManage->setWidgetInfo(UserName);     //设置管理员用户页标签数据
    }else{
        connect(ui->btnUserManage,&QPushButton::clicked,ui->stackedWidget,[=](){
            ui->stackedWidget->setCurrentIndex(4);//设置用户管理页为普通用户
        });
        connect(ui->btnDeviceManage,&QPushButton::clicked,ui->stackedWidget,[=](){
            ui->stackedWidget->setCurrentIndex(1);//设置设备管理页为普通用户
        });
        m_pUserManage->setWidgetInfo(UserName);
    }

}

QString centerMainWidget::getRole()
{
    return user_role;
}
